// 助战工具

export default class HelpFight {

}

/**
 * 根据位置获取角色信息
 * @param {*} client_id 
 * @param {*} index 助战下标
 * @returns 
 */
export function get_help_fight_characters(client_id,index){
    let help_fight = get_help_fight_list(client_id)[index];
    if(help_fight !== undefined){
        return _CharactersData.get(help_fight["id"]);
    }
    return undefined;
}

/**
 * 根据位置获取角色信息
 * @param {*} client_id 
 * @param {*} characters_id 助战id
 * @returns 
 */
export function get_help_fight_characters_by_id(client_id,characters_id){
    let list = get_help_fight_list(client_id);
    for (let j = 0; j < list.length; j++) {
        let characters = list[j];
        if(characters["id"] === characters_id){
            return _CharactersData.get(characters["id"]);
        }
    }
    return undefined;
}

/**
 * 获取当前账号助战列表
 * @param {*} client_id 
 */
export function get_help_fight_list(client_id) {
    let username = _ClientUserNameData.get(client_id);
    let all_characters = G.Characters.get_all_characters(username);
    let data = [];
    let login_characters = G.Characters.get_current_login_characters(client_id);
    all_characters.forEach(characters => {
        if (characters["id"] !== login_characters["id"]) {
            let datai = {
                "名称": characters["名称"],
                "门派": characters["门派"],
                "id": characters["id"],
                "等级": characters["等级"],
                "锦衣": characters["锦衣"].map(e=>{return e !== null ? e["名称"]:null}),
                "炫彩": characters["炫彩"],
                "炫彩组": characters["炫彩组"],
                "染色方案": characters["染色方案"],
                "染色组": characters["染色组"],
                "模型": characters["模型"],
                "变身数据": characters["变身数据"], 
                "装备": characters["装备"][2], 
            };
            //是否在线
            if (G.Characters.check_characters_online(characters["id"])) {
                datai["在线"] = G.Characters.get_characters_online_status(characters);
            }
            data.push(datai);
        }
    });
    return data;
}

/**
 * 创建助战角色
 * @returns 
 */
export async function create_help_fight_characters(client_id,message_json) {
    let username = _ClientUserNameData.get(client_id)
    let uid = _ClientUidData.get(client_id)
    return await G.Characters.create_characters(client_id,uid,username, message_json["名称"], message_json["模型"], message_json["染色ID"]);
}

/**
 * 邀请助战进组
 * @param {*} index 
 */
export function add_characters_to_team(client_id,index) {

    let login_characters = G.Characters.get_current_login_characters(client_id);

    let characters = get_help_fight_characters(client_id,index);

    if(characters === undefined){
        ClientUtils.send_basic_prompt(client_id, "#Y/未找到角色！");
        return;
    }
    if(characters["id"] === login_characters["id"]){
        ClientUtils.send_basic_prompt(client_id,"#Y/不能邀请自己进组");
        return false;
    }
    //判断队伍是否已满
    if (G.TeamUtils.get_team_data(login_characters["队伍"])) {
        if (G.TeamUtils.get_team_data(login_characters["队伍"])["成员"].length >= 5) {
            ClientUtils.send_basic_prompt(client_id, "#Y队伍已满");
            return;
        }
    }
    let level = login_characters["等级"] - characters["等级"];
    if(level >= 100 || level <= -100){
        ClientUtils.send_basic_prompt(client_id, "#Y/等级相差100禁止组队！");
        return
    }

    let status = G.Characters.get_characters_online_status(characters);

    if (status !== "在线" && characters["队伍"] !== login_characters["队伍"]) {
        ClientUtils.send_basic_prompt(client_id, `无法进组,对方正在${status}`);
    } else {
        // 判断助战是否已经在线
        let online = G.Characters.check_characters_online(characters["id"]);
        if (!online) {
            //没上线先做个上线操作
            G.Characters.login(characters["id"]);
        }
        //将助战移入主角色地图
        let map_id = login_characters["地图数据"]["编号"];
        let x = Math.floor(login_characters["地图数据"]["x"]+1);
        let y = Math.floor(login_characters["地图数据"]["y"]+1);
        let res = G.MapUtils.move_characters_to_map(characters,map_id,x,y);

        if(res){
            G.TeamUtils.add_help_fight_to_team(client_id,characters);
            //更新助战列表
            let list = get_help_fight_list(client_id);
            ClientUtils.send_client_message(client_id,2002,list);
        }else{
            if(!online){
                G.Characters.logout_characters(characters["id"]);
            }
        }
        //通知客户端更新队伍信息
        let send_data = G.TeamUtils.build_send_team_data(login_characters["队伍"]);
        ClientUtils.send_client_message(client_id, 4004, send_data);
    }
}


/**
 * 刷新助战面板上数据
 * @param {*} client_id 
 * @param {*} index 
 */
export function refrush_help_fight_characters_data(client_id, index) {
    let characters = get_help_fight_characters(client_id, index);
    let send_data = {
        "装备": characters["装备"],
        "灵饰": characters["灵饰"],
        "锦衣": characters["锦衣"],
        "道具": characters["道具"],
        "助战信息": {
            "助战编号": index+1,
            "id": characters["id"],
            "名称": characters["名称"],
            "模型": characters["模型"],
            "门派": characters["门派"],
        },
    };
    ClientUtils.send_client_message(client_id, 2012, send_data);
}